Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.UI
Imports DevExpress.XtraEditors.Controls

'--- Created Date On : 08-Oct-2010 (Gyana)

Public Class frmSelectPatient

    Private mintPatientId As Integer
    Private mstrReportType As String

    Public Sub New(ByVal TypeReport As String)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

        mstrReportType = TypeReport
    End Sub

    Private Sub frmRegistrationWizard_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Call PopulateLookupDX(lkPractice, "Name", "PracticeId", "tblPracticeInfo JOIN tblUsers ON tblPracticeInfo.UserId=tblUsers.UserId", "PracticeId,(FirstName + ' ' + LastName) AS Name", , "(FirstName + ' ' + LastName)")
            'Call PopulateLookupDX(lkPractice, "Name", "PracticeId", "tblPracticeInfo INNER JOIN tblUsers ON (tblPracticeInfo.UserId = tblUsers.UserId)", "PracticeId,IsNull(tblUsers.FirstName,'') + IsNull(' ' + tblUsers.LastName,'') AS Name")
            Call PopulateLookUpName()

           
            'if doctor is login then disable combobox
            If gintUserLevelId = 1 OrElse gintUserLevelId = 2 Then
                lkPractice.Enabled = False
                lkPractice.EditValue = GetPracticeId(gintUserID)
            End If
            If mstrReportType = "Patient's Profile" Then
                cboDate.Visible = False
                lblVisitDate.Visible = False
            Else
                cboDate.Visible = True
                lblVisitDate.Visible = True
            End If
            Me.Text = mstrReportType
            'dteVisitDate.EditValue = Today

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub
    Private Sub PopulateVisitDate()
        Dim dstemp As DataSet = FillDataSet("SELECT Distinct CONVERT(VARCHAR(15),VisitDate,111) [Visit Date] " & _
                                          " From tblVisits Where PatientId=" & glkPatient.EditValue & " and PracticeId=" & GetPracticeId(gintUserID) & " order by CONVERT(VARCHAR(15),VisitDate,111) desc")
        With cboDate.Properties
            .Columns.Clear()

            .DataSource = dstemp.Tables(0)
            .DisplayMember = "Visit Date"
            .ValueMember = "Visit Date"

            'Add columns to the dropdown.
            Dim coll As LookUpColumnInfoCollection = .Columns


            coll.Add(New LookUpColumnInfo(dstemp.Tables(0).Columns(0).Caption, 0))


            'Set column widths according to their contents.
            .BestFit()

            'Specify the total dropdown width.
            '.PopupWidth = intPopupWidth

            'Enable auto completion search mode.
            .SearchMode = SearchMode.AutoComplete
            'Specify the column against which to perform the search.
            .AutoSearchColumnIndex = 1

            .ShowFooter = False
        End With
    End Sub
    Private Sub btnContinue_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Try
            If IsValidEntry() = False Then Exit Sub

            If mstrReportType = "Invoice" Then
                ShowInvoiceReport(lkPractice.EditValue, glkPatient.EditValue, CDate(cboDate.EditValue), Me.ParentForm)
            ElseIf mstrReportType = "Receipt" Then
                ShowReceiptReport(lkPractice.EditValue, glkPatient.EditValue, CDate(cboDate.EditValue), Me.ParentForm)
            ElseIf mstrReportType = "Visit Details" Then
                ShowVisitDetailsReport(lkPractice.EditValue, glkPatient.EditValue, CDate(cboDate.EditValue), Me.ParentForm)
            ElseIf mstrReportType = "Patient's Profile" Then
                ShowPatientProfileReport(lkPractice.EditValue, glkPatient.EditValue, Me.ParentForm)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub lkPractice_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles lkPractice.ButtonClick
        Try
            If e.Button.Index = 1 Then
                Call PopulateLookupDX(lkPractice, "Name", "PracticeId", "tblPracticeInfo INNER JOIN tblUsers ON (tblPracticeInfo.UserId = tblUsers.UserId)", "PracticeId,IsNull(tblUsers.FirstName,'') + IsNull(' ' + tblUsers.LastName,'') AS Name", , "IsNull(tblUsers.FirstName,'') + IsNull(' ' + tblUsers.LastName,'')")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub glkPatient_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles glkPatient.ButtonClick
        Try
            If e.Button.Index = 1 Then
                Call PopulateLookUpName()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Function IsValidEntry() As Boolean
        'Here check valid entry of fields
        Dim strMsg As String = ""
        IsValidEntry = False

        If IsDBNullOrNothing(lkPractice.EditValue) Then
            strMsg = "Please select Practice"
            lkPractice.Focus()
        ElseIf IsDBNullOrNothing(glkPatient.EditValue) Then
            strMsg = "Please select Patient"
            glkPatient.Focus()
        ElseIf IsDBNullOrNothing(cboDate.Text) And mstrReportType <> "Patient's Profile" Then
            strMsg = "Please select Visit Date"
            cboDate.Focus()
        End If

        If Trim(strMsg) <> "" Then
            IsValidEntry = False
            MessageBox.Show(strMsg, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            IsValidEntry = True
        End If
    End Function

    Private Sub PopulateLookUpName()
        Dim dsTemp As DataSet = FillDataSet("select PatientId,(IsNull(LastName + ', ' , '') + PatientTitle + ' ' + FirstName + Isnull(' ' + MiddleName, '') ) [Patient Name]" & _
                                            ",Address1 [Address],DateOfBirth [Birth Date] from tblPatientInfo Where Void=0")
        With glkPatient.Properties
            .DataSource = dsTemp.Tables(0)
            .DisplayMember = "Patient Name"
            .ValueMember = "PatientId"
            .NullText = ""
            .ImmediatePopup = True
            .ShowFooter = False
            .View.OptionsSelection.EnableAppearanceFocusedCell = False
            .View.OptionsView.AutoCalcPreviewLineCount = True
            .View.OptionsFilter.AllowColumnMRUFilterList = True
            .View.OptionsFilter.AllowFilterEditor = True
            .View.OptionsFilter.AllowMRUFilterList = True
            .View.OptionsView.ShowAutoFilterRow = True
            .View.OptionsView.ShowIndicator = False
            .View.OptionsView.ShowGroupPanel = False
            .View.OptionsView.ShowPreview = True
            .View.OptionsFilter.ShowAllTableValuesInFilterPopup = True
            .ImmediatePopup = True
            .ShowFooter = False
            .View.Columns("PatientId").Visible = False
            .View.Columns("Birth Date").DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime
            .View.Columns("Birth Date").DisplayFormat.FormatString = "MMM dd,yyyy"
        End With
    End Sub

    Private Sub btnSelectPatient_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelectPatient.Click
        Try
            Dim SingleClickOnGridExit As Boolean = True
            Dim SearchPatient As New frmPatientSearch(SingleClickOnGridExit)
            SearchPatient.ShowInTaskbar = False
            If SearchPatient.ShowDialog() = DialogResult.Cancel Then
                Exit Sub
            End If
            glkPatient.EditValue = SearchPatient.PatientIdForAppointment

        Catch ex As Exception
            DestroyCrushor(Me)
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub glkPatient_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles glkPatient.EditValueChanged
        If Not IsDBNullOrNothing(glkPatient.EditValue) Then
            Call PopulateVisitDate()
        End If
    End Sub
End Class
